package com.sskj.level.util;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.level.R;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class TipLevelUtil {
    public static void showTradeInfo(Context context, String direction, String type,
                                     String price, String num, String winPrice, String lowPrice,
                                     String level, String levels, String bzjMoney, String fee, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.level_dialog_trade_info_tip, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((TextView) customeView.findViewById(R.id.tvDirection)).setTextColor(ContextCompat.getColor(App.INSTANCE, "买入".equals(direction) ? R.color.level_05C089 : R.color.level_FF3F3E));
        ((TextView) customeView.findViewById(R.id.tvDirection)).setText(direction);
        ((TextView) customeView.findViewById(R.id.tvType)).setText(type);
        ((TextView) customeView.findViewById(R.id.tvPrice)).setText(price);
        ((TextView) customeView.findViewById(R.id.tvWinPrice)).setText(winPrice);
        ((TextView) customeView.findViewById(R.id.tvLowPrice)).setText(lowPrice);
        ((TextView) customeView.findViewById(R.id.tvLevel)).setText(level);
        ((TextView) customeView.findViewById(R.id.tvNum)).setText(num);
        ((TextView) customeView.findViewById(R.id.tv_pro_money)).setText(bzjMoney);
        ((TextView) customeView.findViewById(R.id.tv_fee)).setText(fee);

        ((ViewGroup) (customeView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        customeView.findViewById(R.id.llLevel).setVisibility(levels == null ? View.GONE : (levels.split(",").length > 1 ? View.VISIBLE : View.GONE));
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.viewCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
    }

    public static MaterialDialog showTradeSet(Context context, String type,
                                              String newPrice,
                                              String stopLossPrice, String stopWinPrice,
                                              String minWinPrice, String maxWinPrice,
                                              String minLossPrice, String maxLossPrice, OnTipResultSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.level_dialog_trade_set_tip, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();
        ((TextView) customeView.findViewById(R.id.etStopLoseDot)).setText(Double.valueOf(stopLossPrice) == 0 ? "" : stopLossPrice);
        ((TextView) customeView.findViewById(R.id.etStopWinDot)).setText(Double.valueOf(stopWinPrice) == 0 ? "" : stopWinPrice);
        ((TextView) customeView.findViewById(R.id.tvType)).setText(type);
        ((TextView) customeView.findViewById(R.id.tvType)).setTextColor(ContextCompat.getColor(App.INSTANCE, App.INSTANCE.getString(R.string.levelCurrentHolderFragment0).equals(type) ? R.color.levelGreenText : R.color.levelRedText));

        ((TextView) customeView.findViewById(R.id.etStopLoseDot)).setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
        ((TextView) customeView.findViewById(R.id.etStopWinDot)).setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
        if (App.INSTANCE.getString(R.string.levelCurrentHolderFragment0).equals(type)) {
            ((TextView) customeView.findViewById(R.id.tvMaxLossPrice)).setText("< " + maxLossPrice);
            ((TextView) customeView.findViewById(R.id.tvMinWinPrice)).setText("> " + minWinPrice);
        } else {
            ((TextView) customeView.findViewById(R.id.tvMaxLossPrice)).setText("> " + minLossPrice);
            ((TextView) customeView.findViewById(R.id.tvMinWinPrice)).setText("< " + maxWinPrice);
        }
        ((TextView) customeView.findViewById(R.id.tvNewPrice)).setText(newPrice);
        EditText etStopLoseDot = customeView.findViewById(R.id.etStopLoseDot);
        EditText etStopWinDot = customeView.findViewById(R.id.etStopWinDot);
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvSure), () -> {
            if (TextUtils.isEmpty(etStopLoseDot.getText()) && TextUtils.isEmpty(etStopWinDot.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil1));
                return;
            }

            if (!TextUtils.isEmpty(etStopWinDot.getText())) {
                if (Double.valueOf(etStopWinDot.getText().toString()) <= Double.valueOf(minWinPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil4) + minWinPrice + "-" + maxWinPrice);
                    return;
                }
                if (Double.valueOf(etStopWinDot.getText().toString()) >= Double.valueOf(maxWinPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil5) + minWinPrice + "-" + maxWinPrice);
                    return;
                }
            }
            if (!TextUtils.isEmpty(etStopLoseDot.getText())) {

                if (Double.valueOf(etStopLoseDot.getText().toString()) >= Double.valueOf(maxLossPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil6) + minLossPrice + "-" + maxLossPrice);
                    return;
                }
                if (Double.valueOf(etStopLoseDot.getText().toString()) <= Double.valueOf(minLossPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil7) + minLossPrice + "-" + maxLossPrice);
                    return;
                }
            }

            dialog.dismiss();
            ArrayList<String> result = new ArrayList<>();
            result.add(etStopWinDot.getText().toString());
            result.add(etStopLoseDot.getText().toString());
            onTipSure.onSure(result);
        });
        return dialog;
    }

    public static void changeTradeSet(MaterialDialog dialog, String type, String newPrice, String minWinPrice, String maxWinPrice,
                                      String minLossPrice, String maxLossPrice, OnTipResultSure onTipSure) {
        View customeView = dialog.getCustomView();
        if (App.INSTANCE.getString(R.string.levelCurrentHolderFragment0).equals(type)) {
            ((TextView) customeView.findViewById(R.id.tvMaxLossPrice)).setText("< " + maxLossPrice);
            ((TextView) customeView.findViewById(R.id.tvMinWinPrice)).setText("> " + minWinPrice);
        } else {
            ((TextView) customeView.findViewById(R.id.tvMaxLossPrice)).setText("> " + minLossPrice);
            ((TextView) customeView.findViewById(R.id.tvMinWinPrice)).setText("< " + maxWinPrice);
        }
        ((TextView) customeView.findViewById(R.id.tvNewPrice)).setText(newPrice);
        EditText etStopLoseDot = customeView.findViewById(R.id.etStopLoseDot);
        EditText etStopWinDot = customeView.findViewById(R.id.etStopWinDot);
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvSure), () -> {
//            if (TextUtils.isEmpty(etStopLoseDot.getText()) && TextUtils.isEmpty(etStopWinDot.getText())) {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil1));
//                return;
//            }
            if (!TextUtils.isEmpty(etStopWinDot.getText())) {
                if (Double.valueOf(etStopWinDot.getText().toString()) <= Double.valueOf(minWinPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil4) + minWinPrice + "-" + maxWinPrice);
                    return;
                }
                if (Double.valueOf(etStopWinDot.getText().toString()) >= Double.valueOf(maxWinPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil5) + minWinPrice + "-" + maxWinPrice);
                    return;
                }
            }
            if (!TextUtils.isEmpty(etStopLoseDot.getText())) {

                if (Double.valueOf(etStopLoseDot.getText().toString()) >= Double.valueOf(maxLossPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil6) + minLossPrice + "-" + maxLossPrice);
                    return;
                }
                if (Double.valueOf(etStopLoseDot.getText().toString()) <= Double.valueOf(minLossPrice)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil7) + minLossPrice + "-" + maxLossPrice);
                    return;
                }
            }
            dialog.dismiss();
            ArrayList<String> result = new ArrayList<>();
            result.add(etStopWinDot.getText().toString());
            result.add(etStopLoseDot.getText().toString());
            onTipSure.onSure(result);
        });
    }

    public static MaterialDialog getTip(Context context, String content, OnTipSure onTipSure) {
        return getTip(context, content, App.INSTANCE.getString(R.string.level_dialog_trade_set_tip130), onTipSure);
    }

    public static MaterialDialog getTip(Context context, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(R.string.level_tip_title)
                .content(content)
                .positiveText(sureMsg)
                .negativeText(R.string.level_cancel)
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String title, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .positiveText(sureMsg)
                .negativeText(R.string.level_cancel)
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public interface OnTipSure {
        void onSure();
    }

    public interface OnTipResultSure {
        void onSure(List<String> list);
    }
}
